<?php

namespace App\Logic\xx;

use App\Logic\BaseLogic;
use App\Server\CommentModel;
use App\Server\FoodCollectionModel;
use App\Server\FoodModel;
use App\Server\MemberModel;

/**
 * Created by PhpStorm.
 * User: hanbin
 * Date: 17/5/31
 * Time: 下午10:10
 */
class FoodLogic extends BaseLogic
{

    /**
     * 菜谱列表
     *
     * @param array $params
     * @return array
     */
    public static function getList($params = [])
    {
        if ($params) {
            $type = $params['type'];
            $pagesize = $params['pagesize'];
            $last_id = $params['last_id'];
            if (isset($params['content'])) {
                if ($type == -1) {
                    $detail = FoodModel::getListLike($pagesize, null, $last_id, $params['content']);
                } else {
                    $detail = FoodModel::getListLike($pagesize, null, $last_id, $params['content'], $type);
                }
            } else {
                if ($type == -1) {
                    $detail = FoodModel::getList($pagesize, null, $last_id, ['status' => 1]);
                } else {
                    $detail = FoodModel::getList($pagesize, null, $last_id, ['type' => $type, 'status' => 1]);
                }
            }
            if ($detail) {
                $list = $detail['data'];
                if ($list) {
                    $result = [];
                    foreach ($list as $k => $v) {
                        $item = $v;
                        unset($item['update_time']);
                        unset($item['status']);
                        $memberId = $v['member_id'];
                        $memberDetail = MemberModel::first(['id' => $memberId]);
                        if ($memberDetail) {
                            $item['header_img'] = $memberDetail['face'];
                            $item['member_name'] = $memberDetail['username'];
                        } else {
                            $item['header_img'] = '';
                            $item['member_name'] = '';
                        }
                        $result[] = $item;
                    }
                    $detail['data'] = $list;
                }
            }
            return self::success($detail, 200, 'ok');
        } else {
            return self::error('请求参数有误', 206);
        }
    }

    /**
     * 详情
     * @param array $params
     * @return array
     */
    public static function detail($params = [])
    {
        if ($params) {
            $member_id = $params['member_id'];
            $id = $params['id'];
            $foodDetail = FoodModel::first(['id' => $id]);
            if ($foodDetail) {
                $item = $foodDetail;
                unset($item['update_time']);
                unset($item['status']);
                $memberId = $foodDetail['member_id'];
                $memberDetail = MemberModel::first(['id' => $memberId]);
                if ($memberDetail) {
                    $item['header_img'] = $memberDetail['face'];
                    $item['member_name'] = $memberDetail['username'];
                } else {
                    $item['header_img'] = '';
                    $item['member_name'] = '';
                }
                $collectDetail = FoodCollectionModel::first(['member_id' => $member_id, 'food_id' => $id, 'is_delete' => 0]);
                if ($collectDetail) {
                    $item['is_collect'] = 1;
                } else {
                    $item['is_collect'] = 0;
                }
                return self::success($item, 200, 'ok');
            } else {
                return self::error('该菜谱不存在', 206);
            }
        } else {
            return self::error('请求参数有误', 206);
        }

    }

    public static function collect($params)
    {
        if ($params) {
            $userId = $params['member_id'];
            $id = $params['id'];
            $detail = FoodCollectionModel::first(['member_id' => $userId, 'food_id' => $id, 'is_delete' => 0]);
            if ($detail) {
                $up = FoodCollectionModel::updated(['id' => $detail['id']], ['is_delete' => 1, 'delete_time' => time()]);
                if ($up) {
                    $foodDetail = FoodModel::first(['id' => $id]);
                    $comment_number = (int)$foodDetail['attention_number'] - 1;
                    $up = FoodModel::updated(['id' => $id], ['attention_number' => $comment_number]);
                    return self::success(['flag' => 0], 200, '取消收藏成功');
                } else {
                    return self::error('取消收藏失败', 206);
                }
            } else {
                $data = [];
                $data['member_id'] = $userId;
                $data['food_id'] = $id;
                $data['create_time'] = time();
                $data['update_time'] = time();
                $up = FoodCollectionModel::add($data);
                if ($up) {
                    $foodDetail = FoodModel::first(['id' => $id]);
                    $comment_number = (int)$foodDetail['attention_number'] + 1;
                    $up = FoodModel::updated(['id' => $id], ['attention_number' => $comment_number]);
                    return self::success(['flag' => 1], 200, '收藏成功');
                } else {
                    return self::error('收藏失败', 206);
                }
            }
        } else {
            return self::error('请求参数有误', 206);
        }
    }

    /**
     * 收藏列表
     *
     * @param $params
     * @param $userId
     * @return array
     */
    public static function getCollectList($params, $userId)
    {

        if ($params && $userId) {
            $pagesize = isset($params['pagesize']) ? $params['pagesize'] : 10;
            $lastId = isset($params['last_id']) ? $params['last_id'] : 1;
            $so['member_id'] = $userId;
            $so['is_delete'] = 0;
            $detail = FoodCollectionModel::getList($pagesize, null, $lastId, $so);
            if ($detail) {
                $list = $detail['data'];
                if ($list) {
                    $result = [];
                    foreach ($list as $k => $v) {
                        $foodDetail = FoodModel::first(['id' => $v['food_id']]);
                        if ($foodDetail) {
                            $item = $foodDetail;
                            unset($item['update_time']);
                            unset($item['status']);
                            $memberId = $v['member_id'];
                            $memberDetail = MemberModel::first(['id' => $memberId]);
                            if ($memberDetail) {
                                $item['header_img'] = $memberDetail['face'];
                                $item['member_name'] = $memberDetail['username'];
                            } else {
                                $item['header_img'] = '';
                                $item['member_name'] = '';
                            }
                            $result[] = $item;
                        }
                    }
                    $detail['data'] = $result;
                }
            }
            return self::success($detail, 200, 'ok');
        } else {
            return self::error('请求参数有误', 206);
        }
    }

    /**
     * 评论
     * @param $params
     * @param $userId
     * @return array
     */
    public static function comment($params, $userId)
    {
        if ($params) {
            $params['member_id'] = $userId;
            $params['status'] = 1;
            $params['create_time'] = time();
            $params['update_time'] = time();
            $up = CommentModel::add($params);
            if ($up) {
                $food_id = $params['food_id'];
                $foodDetail = FoodModel::first(['id' => $food_id]);
                $comment_number = (int)$foodDetail['comment_number'] + 1;
                $up = FoodModel::updated(['id' => $food_id], ['comment_number' => $comment_number]);
                return self::success(['flag' => 1], 200, '评论成功');
            } else {
                return self::success(['flag' => 0], 200, '评论失败');
            }
        } else {
            return self::error('请求参数有误', 206);
        }
    }

    public static function commentList($params)
    {
        if ($params) {
            $detail = CommentModel::getList($params['pagesize'], null, $params['last_id'], ['food_id' => $params['food_id']]);
            if ($detail) {
                $list = $detail['data'];
                if ($list) {
                    $result = [];
                    foreach ($list as $k => $v) {
                        $item = [];
                        $item['id'] = $v['id'];
                        $member_id = $v['member_id'];
                        if ($member_id) {
                            $memberDetail = MemberModel::first(['id' => $member_id]);
                            $item['member_id'] = $memberDetail['id'];
                            $item['member_name'] = $memberDetail['username'];
                        } else {
                            $item['member_id'] = 0;
                            $item['member_name'] = '';
                        }
                        $by_member_id = $v['by_member_id'];
                        if ($by_member_id) {
                            $byMemberDetail = MemberModel::first(['id' => $by_member_id]);
                            $item['by_member_id'] = $byMemberDetail['id'];
                            $item['by_member_name'] = $byMemberDetail['username'];
                        } else {
                            $item['by_member_id'] = 0;
                            $item['by_member_name'] = '';
                        }
                        $item['content'] = $v['content'];
                        $item['create_time'] = $v['create_time'];
                        $result[] = $item;
                    }
                    $detail['data'] = $result;
                }
            }
            return self::success($detail, 200, 'ok');
        } else {
            return self::error('请求参数有误', 206);
        }
    }

    public static function publish($params = [])
    {
        if ($params) {
            $params['image'] = FoodModel::$urls[rand(0, 10)];
            $params['attention_number'] = 0;
            $params['comment_number'] = 0;
            $params['status'] = 1;
            $params['review_number'] = 0;
            $params['create_time'] = time();
            $params['update_time'] = time();
            $up = FoodModel::add($params);
            if ($up) {
                return self::success(['flag' => 1], 200, '发布成功');
            } else {
                return self::success(['flag' => 0], 200, '发布失败');
            }
        } else {
            return self::error('请求参数有误', 206);
        }
    }


    public static function publishList($params = [])
    {
        if ($params) {
            $member_id = $params['member_id'];
            $detail = FoodModel::getList($params['pagesize'], null, $params['last_id'], ['member_id' => $member_id, 'status' => 1]);
            if ($detail) {
                $list = $detail['data'];
                if ($list) {
                    $result = [];
                    foreach ($list as $k => $v) {
                        $item = $v;
                        unset($item['update_time']);
                        unset($item['status']);
                        $memberId = $v['member_id'];
                        $memberDetail = MemberModel::first(['id' => $memberId]);
                        if ($memberDetail) {
                            $item['header_img'] = $memberDetail['face'];
                            $item['member_name'] = $memberDetail['username'];
                        } else {
                            $item['header_img'] = '';
                            $item['member_name'] = '';
                        }
                        $result[] = $item;
                    }
                    $detail['data'] = $list;
                }

                return self::success($detail, 200, 'ok');
            } else {
                return self::error('请求参数有误', 206);
            }
        }
    }
}